import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:myapp/_common/UListTitle.dart';
import 'package:myapp/_views/home/widget/movie/MovieCardList.dart';

// 猜你喜欢 类型 决定显示方式
enum MovieLikeType {
  separate, // 独立
  collection, // 合集
}

class MovieLikeItem {
  final String title; // 标题
  final MovieLikeType type; // 类型
  final String cover; // 背景图
  // 独立类型
  final String? partition; // 分区 独立类型时有效
  final String? style; // 风格 独立类型时有效
  final String? description; // 描述 独立类型时有效
  final Color? barColor; // 信息栏颜色 独立类型时有效
  final String? honor; // 荣誉 独立类型时有效
  // 合集类型
  final List<MovieCard>? cardList; // 卡片列表 合集类型时有效
  final String? subTitle; // 副标题 合集类型时有效
  final Color? backgroundColor; // 背景模糊颜色  合集类型时有效

  MovieLikeItem({
    required this.type,
    required this.title,
    required this.cover,
    this.partition,
    this.style,
    this.description,
    this.barColor,
    this.honor,
    this.cardList,
    this.subTitle,
    this.backgroundColor,
  });
}

/// 影视-猜你喜欢-卡片
class MovieLikeCardItem extends StatelessWidget {
  final MovieLikeItem item;
  const MovieLikeCardItem({super.key, required this.item});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
      child: AspectRatio(
        aspectRatio: item.type == MovieLikeType.separate ? 8 / 6.1 : 5 / 3.95,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: item.type == MovieLikeType.separate
              ? MovieLikeCardItemSeparate(item: item)
              : MovieLikeCardItemCollection(item: item),
        ),
      ),
    );
  }
}

class MovieLikeCardItemButton extends StatelessWidget {
  final String title;
  const MovieLikeCardItemButton({super.key, required this.title});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 3),
      margin: const EdgeInsets.symmetric(horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.2),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Row(
        children: [
          const Icon(
            Icons.star_outline,
            color: Colors.white,
            size: 15,
          ),
          Text(
            title,
            style: const TextStyle(
              color: Colors.white,
              fontSize: 12,
            ),
          )
        ],
      ),
    );
  }
}

class MovieLikeCardItemTag extends StatelessWidget {
  final String partition;
  final String style;
  const MovieLikeCardItemTag(
      {super.key, required this.partition, required this.style});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 0.5),
      margin: const EdgeInsets.only(right: 10),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.2),
        borderRadius: BorderRadius.circular(2),
      ),
      child: Text(
        '$partition·$style',
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
          height: 1.15,
        ),
      ),
    );
  }
}

class MovieLikeCardItemRichText extends StatelessWidget {
  final String? description;
  final String? honor;
  const MovieLikeCardItemRichText({
    super.key,
    this.description,
    this.honor,
  });
  @override
  Widget build(BuildContext context) {
    RichText richText = RichText(
      text: const TextSpan(
        children: [],
      ),
    );
    const Color iconColor = Colors.white60;
    const double iconSize = 13;
    if (description != null) {
      richText = RichText(
        text: TextSpan(
          children: [
            const WidgetSpan(
              child: Icon(
                Icons.format_quote_outlined,
                color: iconColor,
                size: iconSize,
              ),
            ),
            TextSpan(text: description)
          ],
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      );
    } else if (honor != null) {
      richText = RichText(
        text: TextSpan(
          children: [
            const WidgetSpan(
              child: Icon(
                Icons.star_outline,
                color: iconColor,
                size: iconSize,
              ),
            ),
            TextSpan(text: honor),
            const WidgetSpan(
              child: Icon(
                Icons.star_outline,
                color: iconColor,
                size: iconSize,
              ),
            ),
          ],
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      );
    }

    return richText;
  }
}

// 独立类型
class MovieLikeCardItemSeparate extends StatelessWidget {
  final MovieLikeItem item;
  const MovieLikeCardItemSeparate({super.key, required this.item});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomLeft,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(item.cover),
          fit: BoxFit.cover,
        ),
      ),
      child: Container(
        width: double.infinity,
        color: item.barColor,
        padding: const EdgeInsets.fromLTRB(12, 8, 12, 10),
        child: Row(
          children: [
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(bottom: 2),
                    child: Text(
                      item.title,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                  ),
                  Row(
                    children: [
                      MovieLikeCardItemTag(
                        partition: item.partition!,
                        style: item.style!,
                      ),
                      Expanded(
                        child: MovieLikeCardItemRichText(
                          description: item.description,
                          honor: item.honor,
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
            const MovieLikeCardItemButton(title: '追剧'),
            const Icon(
              Icons.more_vert,
              color: Colors.white70,
              size: 14,
            ),
          ],
        ),
      ),
    );
  }
}

// 合集类型
class MovieLikeCardItemCollection extends StatelessWidget {
  final MovieLikeItem item;
  const MovieLikeCardItemCollection({super.key, required this.item});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(item.cover),
              fit: BoxFit.cover,
            ),
          ),
        ),
        BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 3, sigmaY: 2),
          child: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [
                  Colors.transparent,
                  item.backgroundColor ?? Colors.black,
                  item.backgroundColor ?? Colors.black,
                ],
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(12, 0, 12, 2),
                  child: UListTitle(
                    leading: Text(
                      item.title,
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                      ),
                    ),
                    trailing: const Row(
                      children: [
                        Text(
                          '查看更多',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 12,
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 12,
                          color: Colors.white,
                        ),
                      ],
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(12, 0, 12, 6),
                  child: Text(
                    item.subTitle ?? '',
                    style: const TextStyle(
                      color: Colors.white60,
                      fontSize: 13,
                    ),
                  ),
                ),
                MovieCardList(
                  cardList: item.cardList!,
                  size: CardSize.small,
                  titleColor: Colors.white,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
